package daily.may0529;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

class MyCallableThread implements Callable {
    int sum = 0;
    @Override
    public Object call() throws Exception {
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + "当前数值：" + i);
            sum += i;
        }
        return sum;
    }
}

public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //线程1
        FutureTask<Integer> futureTask1 = new FutureTask<>(new MyCallableThread());
        Thread thread1 = new Thread(futureTask1, "线程1");
        thread1.start();
        //线程2
        FutureTask<Integer> futureTask2 = new FutureTask<>(new MyCallableThread());
        Thread thread2 = new Thread(futureTask2, "线程2");
        thread2.start();
        System.out.println("线程1返回值：" + futureTask1.get());
        System.out.println("线程2返回值：" + futureTask2.get());
    }
}
